//
//  PlayViewController.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/15.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "PlayViewController.h"
#import "AppDelegate.h"


@interface PlayViewController ()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *playView;

@end

@implementation PlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self customNavigationBar];
    
    self.playView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.playView.delegate = self;
    _playView.allowsInlineMediaPlayback = YES;
    [self.view addSubview:_playView];
    
    self.playView.scalesPageToFit = YES;
    
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    //不让横屏的时候 appDelegate.allowRotation = NO;即可
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
    
 
    
    

    [self loadRequest];
    

//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.playUrl]];
    
    
    /*
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.playUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
        
        [self.playView loadRequest:request];
        
    });
    */
    
}

#pragma mark - 只允许播放界面横屏
// 进入全屏
-(void)begainFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
    
    
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL landscape = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight);
    NSLog(@"-------进入全屏-------Currently landscape: %@, width: %.2f, height: %.2f",
          (landscape ? @"Yes" : @"No"),
          [[UIScreen mainScreen] bounds].size.width,
          [[UIScreen mainScreen] bounds].size.height);
    
    
}
// 退出全屏
-(void)endFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    
//    //强制归正：
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val =UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    
    
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL landscape = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight);
    NSLog(@"-------退出全屏-------Currently landscape: %@, width: %.2f, height: %.2f",
          (landscape ? @"Yes" : @"No"),
          [[UIScreen mainScreen] bounds].size.width,
          [[UIScreen mainScreen] bounds].size.height);
    
  
    
    NSLog(@"退出全屏");
}

#pragma mark - 请求数据
- (void)loadRequest
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.playUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    [self.playView loadRequest:request];
}

- (void)customNavigationBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bg_green_2@2x"] forBarMetrics:UIBarMetricsDefault];
    
    
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"set_arrow_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popback)];
    [backBarButton setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBarButton;
    
    UIBarButtonItem *refreshBarButton= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
    [refreshBarButton setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = refreshBarButton;
}

- (void)refresh
{
    [self loadRequest];
}

- (void)popback
{
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - webView代理方法
//- (void)webViewDidFinishLoad:(UIWebView *)webView
//{
//    [MBProgressHUD hideHUD];
//}
//
//- (void)webViewDidStartLoad:(UIWebView *)webView
//{
//    [MBProgressHUD showMessage:@"正在加载..."];
//}
//
//- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
//{
//    [MBProgressHUD hideHUD];
//}

@end
